package com.boot.security.controller;

import com.boot.persistence.controller.association.AssociationControllerImpl;
import com.boot.security.model.po.Access;
import com.boot.security.model.po.AccessResource;
import com.boot.security.model.po.Resource;
import com.boot.security.service.database.AccessResourceService;
import com.boot.web.http.HttpModel;
import com.boot.web.util.HttpResponseUtil;
import io.swagger.annotations.Api;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Validator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author 霜寒 <1621856595@qq.com>
 * @date 2020/04/02 20:25
 **/

@RestController
@RequestMapping("/access-resource")
@Api("权限-资源接口")
public class AccessResourceController extends AssociationControllerImpl<Long, Long, Long, AccessResource, Access, Resource,
        AccessResourceService> {

    public AccessResourceController(AccessResourceService service, Validator validator) {
        super(service, validator);
    }

    @Deprecated
    @PostMapping("/authority")
    ResponseEntity<HttpModel<Set<String>>> searchPathAuthority(@RequestBody Map<String, String> body) {
        String path = body.get("path");
        return HttpResponseUtil.OK("查询成功", service.searchPathAuthority(path));
    }

    @PostMapping("/authorities")
    ResponseEntity<HttpModel<Map<String, Set<String>>>> searchPathAuthorities(@RequestBody List<String> paths) {
        Map<String, Set<String>> result = new HashMap<>((int) (paths.size() / .75 + 1));
        for (String path : paths) {
            result.put(path, service.searchPathAuthority(path));
        }
        return HttpResponseUtil.OK("查询成功", result);
    }

}
